// compontents/login.js
const App = getApp()
import { loginModel } from '../../models/login.js'
import { encodeUnicode } from '../../utils/index.js'
const ModelLogin = new loginModel()
import { AppModel } from '../../models/app.js'
const ModelApp = new AppModel()
// var gio = require('../../utils/gio-minp.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    isauth: false,
    locked: false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar: true
  },
  ready: function() {
    console.log('登陆组件ready')
    // 用户是否授权
    const _this = this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          _this.setData({
            isauth: true
          })
          console.log('用户已经授权isauth=', _this.data.isauth)
        }
      },
      fail(err) {}
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    login() {
      console.log('用户点击登录')
      this.wechatSignUp()
    },
    // 隐藏
    success() {
      this.triggerEvent('login')
    },
    // 登陆注册
    wechatSignUp() {
      console.log(' 登录 wechatSignUp')
      // 上锁如果正在请求接口那么就返回
      if (this.data.locked) {
        return
      }
      this.data.locked = true
      //注册或者登陆获取token
      let code = ''
      App.WxService.login()
        .then(data => {
          code = data.code
          wx.setStorageSync('logincode', data.code)
          console.log(' logincode')
          return App.WxService.getUserInfo()
        })
        .then(data => {
          // 记录用户信息
          console.log('data.userInfo', data.userInfo)
          // 数据统计
          App.globalData.gio('setVisitor', data.userInfo)
          /*encrypteData注意*/
          return ModelLogin.wechatSignUp({
            encrypteData: data.encryptedData,
            iv: data.iv,
            rawData: encodeUnicode(data.rawData), // 编码
            signature: data.signature,
            code: code
          })
        })
        .then(data => {
          this.data.locked = false
          console.log('login utoken==', data.data.token)
          wx.setStorageSync('utoken', data.data.token)
          ModelApp._getVipInfo().then(res => {
            App.globalData.userInfo = res.data.userInfo
            // ucode
            wx.setStorageSync('ucode', res.data.userInfo.userCode)
            // 数据统计
            App.globalData.gio('setUserId', res.data.userInfo.id)
            App.globalData.gio('identify', res.data.userInfo.openId)
            // if (res.data.userInfo.isActive == 1) {
            //   wx.showToast({ title: '无法登陆', icon: 'none',  duration: 3000 })
            //   wx.removeStorageSync('utoken')
            //   return false
            // }
            if (res.data.userInfo.isBrand == 0) {
              // 绑定上一级
              ModelApp.addBossFans()
                .then(res => {
                  // 绑定成功更新 App.globalData.userInfo
                  ModelApp._getVipInfo().then(res => {
                    App.globalData.userInfo = res.data.userInfo
                  })
                })
                .catch(err => {
                  App.globalData.bug.notifyError(JSON.stringify(err))
                })
            }
            // 成功
            this.success()
          })
        })
        .catch(err => {
          this.data.locked = false
          console.log('wechatSignUp err', JSON.stringify(err))
          App.globalData.bug.notifyError(JSON.stringify(err))
        })
    }
  }
})
